<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机器人" prop="robotId">
        <el-select v-model="queryParams.robotId" placeholder="请选择机器人" clearable size="small">
          <el-option v-for="item in robotOptions" :key="item.name" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="回测数据数量" prop="barCount">
        <el-input v-model="queryParams.barCount" placeholder="默认1000" clearable size="small" style="width: 240px"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleTest">回测</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="bucketList">
      <el-table-column label="交易对" align="center" prop="symbol"/>
      <el-table-column label="K线间隔" align="center" prop="interval"/>
      <el-table-column label="交易次数" align="center" prop="tradeTimes"/>
      <el-table-column label="交易成功率(%)" align="center" prop="profitTradeRatio"/>
      <el-table-column label="收益率(%)" align="center" prop="profitRatio"/>
      <el-table-column label="购持率(%)" align="center" prop="buyAndHoldRatio"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getRobots, testRobot,searchBackTest } from "@/api/trade/bucket";

export default {
  name: "Bucket",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 回测结果表格数据
      bucketList: [],
      // 交易所数据
      robotOptions: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sortField: "profit_ratio",
        robotId: null,
        barCount: 1000
      }
    };
  },
  created() {
    this.getRobots();
  },
  methods: {
    /** 获取机器人列表 */
    getRobots() {
      getRobots().then(response => {
        this.robotOptions = response.robots;
        this.loading = false;
      });
    },
    /** 获取回测结果列表 */
    getList() {
      this.loading = true;
      searchBackTest(this.queryParams).then(
        response => {
          this.bucketList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    /** 回测按钮操作 */
    handleTest() {
      testRobot(this.queryParams);
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
    }
  }
};
</script>